package tech.wetech.admin.modules.system.web;

import com.github.pagehelper.Page;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.dao.DataAccessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import tech.wetech.admin.core.annotation.SystemLog;
import tech.wetech.admin.core.utils.Result;
import tech.wetech.admin.modules.base.query.PageQuery;
import tech.wetech.admin.modules.base.web.BaseCrudController;
import tech.wetech.admin.modules.system.enums.Channel;
import tech.wetech.admin.modules.system.enums.PayPattern;
import tech.wetech.admin.modules.system.po.Company;
import tech.wetech.admin.modules.system.service.CompanyService;

import java.util.ArrayList;
import java.util.List;

/**
 * @author lu
 * @description 商户管理
 * @date 2019年06月27日 13:13
 */
@Controller
@RequestMapping("/company")
public class CompanyController extends BaseCrudController<Company> {

    @Autowired
    private CompanyService companyService;

    @ModelAttribute("types")//
    public Channel[] resourceTypes() {
        return Channel.values();
    }

    @ModelAttribute("paytypes")//
    public PayPattern[] paytypes() {
        return PayPattern.values();
    }

    @GetMapping
    public String company(Model model) {
        return "system/company";
    }

    @Autowired
    private RedisTemplate<String, String> stringredisTemplate;

    @ResponseBody
    @PostMapping("/applyifo")
    public int applyInfo(String mno) {
        System.out.println(mno);
        return 1;
    }

    @ResponseBody
    @GetMapping("/list")
    @Override
    public Result<List<Company>> queryList(Company company, PageQuery pageQuery) {
        List<Company> company1 = companyService.querySelectList(company, pageQuery);
        List<Company> companys =new ArrayList<>();
        company1.forEach(c -> {
            c.setPayCompanyInfos(companyService.selectpayCompanyInfo(c.getCompanyId()));
            companys.add(c);
        });
        return Result.success(companys)
                .addExtraIfTrue(pageQuery.isCountSql(), "total", ((Page) company1).getTotal());
    }

    @ResponseBody
    @PostMapping("/add")
    @RequiresPermissions("company:create")
    @SystemLog("商户管理新增")
    @Override
    public Result create(Company company) {
        companyService.createCompany(company);
        return Result.success();
    }

    @ResponseBody
    @PostMapping("/update")
    @RequiresPermissions("company:update")
    @SystemLog("商户管理修改")
    @Override
    public Result update(Company company) {
        companyService.updateCompany(company);
        flushdb();
        return Result.success();
    }

    @ResponseBody
    @SystemLog("商户管理删除")
    @RequiresPermissions("company:delete")
    @PostMapping("/delete-batch")
    @Override
    public Result deleteBatchByIds(@RequestParam("id") Object[] ids) {
        companyService.deleteBatchByIds(ids);
        flushdb();
        return Result.success();
    }

    /**
     * @author lu
     * @date 2020/4/7 15:37
     * @param: 
     * @return void
     * @description 清空当前redis数据库
    */
    public void flushdb() {
        stringredisTemplate.execute(new RedisCallback<Object>() {
            @Override
            public String doInRedis(RedisConnection connection) throws DataAccessException {
                connection.flushDb();
                return "ok";
            }
        });
    }
}
